﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for BaohanhController
/// </summary>
public class BaohanhController:Interface
{
    private SqlConnection con;
	public BaohanhController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Baohanh";
            cmd.CommandType = CommandType.StoredProcedure;
            Baohanh bh = (Baohanh)obj;
            cmd.Parameters.Add("@title", SqlDbType.NText).Value = bh.Title;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = bh.Description;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = bh.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = bh.Status;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = bh.DateStart;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_Baohanh";
            cmd.CommandType = CommandType.StoredProcedure;
            Baohanh bh = (Baohanh)obj;
            cmd.Parameters.Add("@baohanh_id", SqlDbType.Int).Value = bh.Baohanh_id;
            cmd.Parameters.Add("@title", SqlDbType.NText).Value = bh.Title;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = bh.Description;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = bh.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = bh.Status;
            cmd.Parameters.Add("@dateUpdate", SqlDbType.DateTime).Value = bh.DateUpdate;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_Baohanh";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@baohanh_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public System.Data.DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Baohanh WHERE [status]=1 ORDER BY [order] ASC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public System.Data.DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Baohanh WHERE baohanh_id=@baohanh_id AND [status]=1 ORDER BY [order] ASC";
            cmd.Parameters.AddWithValue("@baohanh_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public System.Data.DataTable GetByProductId(int productid)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select Baohanh.title from Baohanh inner join Product on Baohanh.baohanh_id=Product.baohanh_id AND Product.product_id=@productId";
            cmd.Parameters.AddWithValue("productId",productid);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {


            throw;
        }
    }
}